var path;
var doing;
var couponType;
$(document).ready(function(){
	
	// init
	init();
	
	// select coupons
	$("#J_logisticsList input[name='selectedCoupons']").live("click",selectCoupons);
	
	// use coupons
	$(".addNewAddressBtn").on("click",useCoupons);
});

function init(){
	path = $("#path").val();
	couponType = $("#couponType").val();
	doing = false;
}

function selectCoupons(){
	var cur = $(this);
	$("#J_logisticsList input[name='selectedCoupons']").not(cur).prop("checked",false);
}

function useCoupons(){
	if(doing){
		return;
	}
	doing = true;
	doUseCoupons();
	doing = false;
}

function doUseCoupons(){
	// 获取选中的优惠券
	var selectCoupons = $("#J_logisticsList input[name='selectedCoupons']:checked");
	var couponId = null;
	if(selectCoupons.length > 0){
		// 当前选中的优惠券的ID
		couponId = selectCoupons.parents("li").find("input[name='couponId']").val();
	}
	$("body").find("form[name='use-coupon-form']").remove();
	
	var formTag = "<form method='post' name='use-coupon-form' action='"+path+"/coupon/use'></form>";
	$("body").append(formTag);
	
	var useCouponForm = $("body").find("form[name='use-coupon-form']");
	if(couponId != null){
		useCouponForm.append("<input type='hidden' name='couponId' value='"+couponId+"'/>");
	}
	useCouponForm.append("<input type='hidden' name='couponType' value='"+couponType+"'/>");
	
	useCouponForm.submit();
}